package com.atguigu.champter7.state;

import com.atguigu.beans.WaterSensor;
import com.atguigu.utils.AtguiguUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Comparator;
import java.util.List;

public class Flink05_KeyedState_Value {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("reset.port", 9999);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        env
                .socketTextStream("hadoop104", 9999)
                .map(line -> {
                    String[] watersensors = line.split(",");
                    return new WaterSensor(watersensors[0], Long.valueOf(watersensors[1]), Integer.valueOf(watersensors[2]));
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                //最大乱序程度
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                //分配水印
                                .withTimestampAssigner((element, ts) -> element.getTs())

                                .withIdleness(Duration.ofSeconds(5))//如果有个并行度长时间[自定义时间]未获取数据,将另一个并行度的水印给没有数据的并行度
                )
                .keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {

                    private ValueState<WaterSensor> vcgt10;

                    //检测传感器的水位值，如果连续的两个水位值超过10，就输出报警
                    @Override
                    public void open(Configuration parameters) throws Exception {
                        vcgt10 = getRuntimeContext().getState(new ValueStateDescriptor<WaterSensor>("Vcgt10", WaterSensor.class));
                    }

                    @Override
                    public void processElement(WaterSensor value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {
                        if (vcgt10.value() != null){
                            if (value.getVc() > 10 && vcgt10.value().getVc() > 10){
                                out.collect( "警告!!!警告!!!,连续两个水位超过10,现在水位为 : " + value);
                            }
                        }

                        vcgt10.update(value);
                    }
                })
                .print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
